from django.db import models
from .Dtime import get_now_time


class QueryDatabase(models.Model):
    s_address = models.GenericIPAddressField("源数据库地址", max_length=64, help_text="数据库地址")
    s_types = models.CharField("源数据库类型", max_length=20, help_text="数据库类型")
    s_user = models.CharField("源数据库账号", max_length=64, help_text="账号")
    # s_passwd = models.CharField("源数据库密码", max_length=64, help_text="密码")
    s_port = models.CharField("源数据库端口", max_length=64, help_text="端口")
    d_address = models.GenericIPAddressField("目的数据库地址", max_length=64, help_text="数据库地址")
    d_types = models.CharField("目的数据库类型", max_length=20, help_text="数据库类型")
    d_user = models.CharField("目的数据库账号", max_length=64, help_text="账号")
    # d_passwd = models.CharField("目的数据库密码", max_length=64, help_text="密码")
    d_port = models.CharField("目的数据库端口", max_length=64, help_text="端口")
    # task_detail = models.OneToOneField(to="RunSqlTask", on_delete=models.CASCADE)

    class Meta:
        db_table = "des_database_info"
        # managed = False

    def __str__(self):
        return self.s_address


class RunSqlTask(models.Model):
    t_taskid = models.CharField("任务id", max_length=64, help_text="任务id")
    t_starttime = models.CharField("任务开始时间", max_length=64, default=get_now_time(), help_text="任务开始时间")
    t_endtime = models.CharField("任务结束时间", max_length=64, help_text="任务结束时间")
    t_datanumeber = models.CharField("完成数据量", max_length=64, null=True, help_text="完成数据量")
    t_dbinfo = models.JSONField("表字段", max_length=64, null=True, help_text="表字段")
    t_status = models.CharField("任务状态", max_length=64, help_text="任务状态")
    database = models.ForeignKey(QueryDatabase, verbose_name="数据库信息", on_delete=models.CASCADE)

    class Meta:
        db_table = "des_task_info"

    def __str__(self):
        return self.t_taskid



